Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS Runtime middleware example #6

Merged
merged 2 commits into from
Feb 6, 2025
Merged

NextJS Runtime middleware example #6

merged 2 commits into from
Feb 6, 2025

Conversation

Brandawg93
Copy link
Contributor

I hope this helps with the GODOXY_API_ADDR issue.

The issue is that the configuration in next.config.js rewrites at compile time and not runtime. In order to be able to dynamically change the address, you would need to use middleware.

This PR is an example of using the middleware to proxy the request to the api. I have deliberately left the debug logging in because I'm still having a connection refused issue. But I believe it may be an issue with my own personal setup. I'll continue to debug on my side, but I believe you may have a more comprehensive knowledge of the api portion. 😄

Let me know if you have any questions or if I can help further!

@Brandawg93
Copy link
Contributor Author

Got it working by using GODOXY_API_ADDR=0.0.0.0:8888

Copy link

sonarqubecloud bot commented Feb 6, 2025

@yusing
Copy link
Owner

yusing commented Feb 6, 2025

WoW, nice! Thanks a lot!

@Brandawg93
Copy link
Contributor Author

No problem. I forgot to mention that I had to use two separate variables for the frontend and backend. Here is my modified compose.

services:
  godoxy-frontend:
    image: brandawg93/go-proxy-frontend:local
    container_name: godoxy-frontend
    restart: unless-stopped
    env_file: .env
    environment:
      - GODOXY_API_ADDR=http://godoxy:8888
    depends_on:
      - godoxy
  godoxy:
    image: ghcr.io/yusing/go-proxy:latest
    container_name: godoxy
    restart: always
    env_file: .env
    environment:
      - GODOXY_API_ADDR=0.0.0.0:8888

The image used for the frontend is just a local build I created. And I redacted other parts of my compose, but you get the gist.

@yusing
Copy link
Owner

yusing commented Feb 6, 2025

Oh, I expect, and GoDoxy currently only works in host network mode. So using the same value should be fine since the API expose sensitive info and should only be listening on 127.0.0.1.

@yusing yusing merged commit 512ac60 into yusing:main Feb 6, 2025
3 checks passed
@Brandawg93
Copy link
Contributor Author

Using this I have it working in bridge mode. 😀

@yusing
Copy link
Owner

yusing commented Feb 6, 2025

I got it working ac2b040:

# godoxy/compose.yml
services:
  frontend:
    ...
    network_mode: host
    env_file: .env
    environment:
      NEXT_PUBLIC_GODOXY_API_ADDR: ${GODOXY_API_ADDR}
    ...

@Brandawg93
Copy link
Contributor Author

Nice! Do you need the NEXT_PUBLIC_ ? That would just make it available on the client side but you only need that variable in the middleware.

https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser

@yusing
Copy link
Owner

yusing commented Feb 6, 2025

Oh you're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants